
package lpr.minikazaa.minikazaaclient.supernode;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import lpr.minikazaa.minikazaaclient.NodeConfig;

/**
 *
 * @author  Andrea Di Grazia, Massimiliano Giovine
 * @date 17/08/2008
 * @file SupernodeWarning.java
 */
public class SupernodeWarning extends javax.swing.JFrame {

    NodeConfig new_config;

    /** Crea nuovo form SupernodeWarning */
    public SupernodeWarning(String warning_str, final String option, NodeConfig c) {
        new_config = c;
        initComponents();
        warning_message.setText(warning_str);

        //Modifica i cambiamenti nella text field secondo le opzioni passate
        if (option.equals("bs_address")) {
            change.setText("New Bootstrap server address:");
        } else {
            if (option.equals("user")) {
                change.setText("New user name:");
            } else {
                if (option.equals("port")) {
                    change.setText("New port:");
                } else {
                    if (option.equals("max_conn")) {
                        change.setText("New max connection number:");
                    } else {
                        if (option.equals("ttl")) {
                            change.setText("New time to leave:");
                        }
                    }
                }
            }
        }

        ok_bt.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent event) {
                        if (change_tf.getText().equals("")) {
                            //Do nothing
                            return;
                        } else {
                            if (option.equals("bs_address")) {
                                new_config.setBootStrapAddress(change_tf.getText());

                            } else {
                                if (option.equals("port")) {
                                    new_config.setPort(Integer.parseInt(change_tf.getText()));
                                } else {
                                    if (option.equals("user")) {
                                        new_config.setUserName(change_tf.getText());
                                    } else {
                                        if (option.equals("max_conn")) {
                                            new_config.setMaxConnection(Integer.parseInt(change_tf.getText()));
                                        } else {
                                            if (option.equals("ttl")) {
                                                new_config.setTimeToLeave(Integer.parseInt(change_tf.getText()));
                                            }
                                        }
                                    }
                                }
                            }
                            //Scrivi file xml
                            XMLEncoder config_xml;
                            try {

                                config_xml = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("config.xml")));
                                config_xml.writeObject((Object) new_config);
                                config_xml.flush();
                                config_xml.close();
                            } catch (FileNotFoundException ex) {
                            }
                            //chiudi
                            dispose();
                        }
                    }
                });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        warning_message = new javax.swing.JLabel();
        ok_bt = new javax.swing.JButton();
        change = new javax.swing.JLabel();
        change_tf = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("Super Node Warning");

        warning_message.setFont(new java.awt.Font("DejaVu Sans", 1, 12)); // NOI18N
        warning_message.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lpr/minikazaa/icons/warning_icon.png"))); // NOI18N

        ok_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lpr/minikazaa/icons/ok_icon.png"))); // NOI18N
        ok_bt.setText("Ok");

        change.setText("Modif");

        jLabel2.setText("Modifica il campo e riavvia il programma.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(warning_message)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(ok_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(97, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addComponent(change, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(change_tf, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25))
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(jLabel2)
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addComponent(warning_message)
                .addGap(25, 25, 25)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(change))
                    .addComponent(change_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(ok_bt)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel change;
    private javax.swing.JTextField change_tf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton ok_bt;
    private javax.swing.JLabel warning_message;
    // End of variables declaration//GEN-END:variables
}
